package org.example.medical.service.impl;

import org.example.medical.Mapper.SurgeryRecordMapper;
import org.example.medical.Mapper.UserMapper;
import org.example.medical.Mapper.VisitRecordMapper;
import org.example.medical.entity.User;
import org.example.medical.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SurgeryRecordMapper surgeryRecordMapper;
    @Autowired
    private VisitRecordMapper visitRecordMapper;
    // 查询用户记录分页数据
    @Override
    public List<User> getUsers(String username, String role, int page, int size) {
        int offset = (page - 1) * size;
        return userMapper.selectByPage(username, role, offset, size);
    }

    // 新增用户记录
    @Override
    public void addUser(User user) {
        // 密码加密
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        user.setPassword(encoder.encode(user.getPassword()));
        userMapper.insertUser(user);
    }

    // 更新用户记录
    @Override
    public void updateUser(User user) {
        // 密码加密
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        user.setPassword(encoder.encode(user.getPassword()));
        userMapper.updateUser(user);
    }

    // 软删除用户记录
    @Override
    public void softDeleteUser(Integer userId) {
        List<Long> surgeryIds = surgeryRecordMapper.getSurgeryIdsByUserId(userId);
        List<Long> visitIds = visitRecordMapper.getRecordIdsByUserId(userId);
        if (visitIds != null && !visitIds.isEmpty()){
            visitRecordMapper.batchSoftDeleteVisitRecords(visitIds);
        }
        if (surgeryIds != null && !surgeryIds.isEmpty()){
            surgeryRecordMapper.batchSoftDeleteSurgeryRecords(surgeryIds);
        }
        userMapper.softDeleteUser(userId);
    }

    // 批量软删除用户记录
    @Override
    public void batchSoftDeleteUsers(List<Integer> userIds) {
        for(Integer userId : userIds){
            List<Long> surgeryIds = surgeryRecordMapper.getSurgeryIdsByUserId(userId);
            List<Long> visitIds = visitRecordMapper.getRecordIdsByUserId(userId);
            if (visitIds != null && !visitIds.isEmpty()){
                visitRecordMapper.batchSoftDeleteVisitRecords(visitIds);
            }
            if (surgeryIds != null && !surgeryIds.isEmpty()){
                surgeryRecordMapper.batchSoftDeleteSurgeryRecords(surgeryIds);
            }
        }
        userMapper.batchSoftDeleteUsers(userIds);
    }

    // 用户注册
    @Override
    public void registerUser(User user) {
        // 密码加密
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        user.setPassword(encoder.encode(user.getPassword()));
        userMapper.registerUser(user);
    }

    // 用户登录
    @Override
    public User loginUser(String username, String password) {
        // 先查询用户信息（不包括密码）
        User user = userMapper.findUserByUsername(username);
        if (user != null && user.getIsDeleted() == 0) {
            // 验证密码
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            if (encoder.matches(password, user.getPassword())) {
                return user;
            }
        }
        return null;
    }

}
